//
//  GRStarsView.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/26.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRStarsView.h"
#import "GRStartView.h"
#define starNum 5
@interface GRStarsView()
@property(nonatomic,strong)NSMutableArray * starViewArr;
@property(nonatomic,assign)CGFloat starW;
@end
@implementation GRStarsView

-(void)awakeFromNib{
    [super awakeFromNib];
    _isGesSelect = YES;
    [self setUI];
}

-(void)setUI{
    _starViewArr = [NSMutableArray array];
    _starW = self.bounds.size.height;
    CGFloat margin  = (self.bounds.size.width - _starW *starNum)/(starNum-1);
    CGFloat starH = self.bounds.size.height;
    for (int i = 0; i <starNum; i++) {
        GRStartView * startView = [[GRStartView alloc]initWithFrame:CGRectMake((margin + _starW)*i, 0, _starW, starH)];
        startView.backgroundColor = [UIColor clearColor];
        [startView setPercent:0.0 IsGray:NO];
        [self addSubview:startView];
        [_starViewArr addObject:startView];
        
    }
}

-(void)setScore:(CGFloat)score{
    _score = score;
    if (score >= _starViewArr.count) {
        _score = _starViewArr.count;
    }else if(score <= 0){
        _score = 0;
    }
    NSInteger index = (NSInteger)_score;
     GRStartView *starView = index == _starViewArr.count?nil:_starViewArr[index];
    for (int i = 0; i < _starViewArr.count; i ++) {
        GRStartView *star = _starViewArr[i];
        if (i < index) {
            [star setPercent:1.0 IsGray:NO];
        }else if(i > index){
            [star setPercent:0.0 IsGray:NO];
        }else{
            CGFloat percent = _score - index;
            [starView setPercent:percent IsGray:NO];
        }
    }
    
    
    
}

- (void)handleTouches:(NSSet *)touches{
    if (!_isGesSelect) {
        return;
    }
    
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    GRStartView *starView;

    for (GRStartView *star in _starViewArr) {
        if (star.frame.origin.x <= point.x && star.frame.origin.x + star.frame.size.width+_starW >= point.x) {
            starView = star;
            break;
        }
    }

    if (!starView) {
        return;
    }
    NSInteger index = [_starViewArr indexOfObject:starView];
    CGFloat Score = index;
    for (int i = 0; i < _starViewArr.count; i ++) {
        GRStartView *star = _starViewArr[i];
        if (i < index) {
            if (i <= 1 && _isComment&& index <=1) {
                [star setPercent:1.0 IsGray:YES];
            }else{
                [star setPercent:1.0 IsGray:NO];
            }
        }else if(i > index){
           [star setPercent:0.0 IsGray:NO];
        }else{
            if(_supportDecimal){
                CGFloat percent = (point.x - starView.frame.origin.x)/starView.frame.size.width;
                [starView setPercent:percent IsGray:NO];
                Score = (Score -i)+percent;
            }else{
                Score = i;
                if (index <=1&&_isComment&& index <=1) {
                    [star setPercent:1.0 IsGray:YES];
                }else{
                      [star setPercent:1.0 IsGray:NO];
                }
            }
        }
    }
    if (_delegate && [_delegate respondsToSelector:@selector(selectedStarScore:)]) {
        [_delegate selectedStarScore:Score+1];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self handleTouches:touches];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [self handleTouches:touches];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    [self handleTouches:touches];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self handleTouches:touches];
}

@end
